<template>
  <div class="login-container">
    <el-form ref="form"
             :model="form"
             :rules="rules"
             class="login-form"
             auto-complete="off"
             label-position="left">

      <div class="title-container">
        <h3 class="title">用户注册</h3>
      </div>

      <el-form-item prop="credential">
        <span class="svg-container">
          <i class="el-icon-s-custom"></i>
        </span>
        <el-input ref="credential"
                  name="credential"
                  v-model="form.credential"
                  placeholder="请输入用户名"
                  type="text"
                  tabindex="1"
                  auto-complete="on" />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <i class="el-icon-lock"></i>
        </span>
        <el-input :key="passwordType"
                  :type="passwordType"
                  ref="password"
                  name="password"
                  v-model="form.password"
                  placeholder="请输入密码"
                  tabindex="2"
                  auto-complete="off"
                  @keyup.enter.native="handleRegister" />
      </el-form-item>

      <el-form-item prop="passwordConfirm">
        <span class="svg-container">
          <i class="el-icon-lock"></i>
        </span>
        <el-input :key="passwordType"
                  :type="passwordType"
                  ref="passwordConfirm"
                  name="passwordConfirm"
                  v-model="form.passwordConfirm"
                  placeholder="请输入密码确认"
                  tabindex="2"
                  auto-complete="off"
                  @keyup.enter.native="handleRegister" />
      </el-form-item>

      <el-form-item prop="phone">
        <span class="svg-container">
          <i class="el-icon-mobile"></i>
        </span>
        <el-input ref="phone"
                  name="phone"
                  v-model="form.phone"
                  placeholder="请输入手机号码"
                  type="text"
                  tabindex="1"
                  auto-complete="on" />
      </el-form-item>

      <!-- <el-row>
        <el-col :span="15">
          <el-form-item prop="messagecaptcha">
            <el-input ref="messagecaptcha"
                      name="messagecaptcha"
                      type="text"
                      v-model="form.messagecaptcha"
                      placeholder="请输入短信验证码"
                      tabindex="3"
                      auto-complete="off"
                      @keyup.enter.native="handleRegister">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8"
                :push="1">
          <el-button id="smsCaptchaBtn"
                     :disabled="smsCaptcha.smsCaptchaDisabled"
                     @click="sendSms">{{smsCaptcha.smsCaptchaBtnText}}</el-button>
        </el-col>
      </el-row> -->

      <!-- <el-row>
        <el-col :span="15">
          <el-form-item prop="smscaptcha">
            <span class="svg-container">
              <svg-icon icon-class="phone" />
            </span>
            <el-input ref="smscaptcha"
                      name="smscaptcha"
                      type="text"
                      v-model="form.smscaptcha"
                      placeholder="请输入图形验证码">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8"
                :push="1">
          <img src="http://localhost:5000/api/captcha/img"
               style="height:35px; margin: 0 0 5px 20px;"
               title="看不清? 点击更换"
               onclick="this.src = 'http://localhost:5000/api/captcha/img?t=' + Math.random()" />
        </el-col>
      </el-row> -->

      <el-row type="flex" class="row-bg" justify="end">
            <el-col :span="2">
              <el-button type="text" @click="$router.go(-1)">返回</el-button>
            </el-col>
      </el-row>

      <el-button :loading="loading"
                 type="primary"
                 style="width:100%;margin-bottom:30px;"
                 @click.native.prevent="handleRegister">注册</el-button>

    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      smsCaptcha: {
        enable: false,
        smsCaptchaGet: false,
        smsCaptchaTick: 59,
        smsCaptchaDisabled: false,
        smsCaptchaBtnText: '获取短信验证码',
      },
      form: {
        credential: '',
        password: '',
        passwordConfirm: '',
        messagecaptcha: '',
        phone: ''
        //imgcaptcha: '',
      },
      rules: {
        credential: [{ required: true, message: '请输入正确的用户名或手机号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        passwordConfirm: [{ required: true, trigger: 'blur', validator: validatePass2 }],
        messagecaptcha: [{ required: true, message: '请输入短信验证码', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入手机号码', trigger: 'blur' }],
        //imgcaptcha: [{ required: true, message: '请输入图形验证码', trigger: 'blur' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {

  },
  methods: {
    handleRegister () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          console.log(1)
          this.$store.dispatch('user/register', this.form).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch((ex) => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  #smsCaptchaBtn {
    width: 140px;
    float: right;
    margin-top: 7px;
    color: #ffffff;
    background-color: #fdc26c;
    border: none;
  }

  #smsCaptchaBtn:disabled {
    color: #ffffff;
    background-color: #b4b4b4;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-image: url("https://gw.alipayobjects.com/zos/rmsportal/TVYTbAXWheQpRcWDaDMu.svg");

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
